//
//  LYNavigationBarView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/16.
//

import UIKit
import SnapKit

class LYNavigationBarView: UIView {
    
    // MARK: - Properties
    
    // 标题
    var title: String? {
        didSet {
            titleLabel.text = title
        }
    }
    
    // 标题视图
    var titleView: UIView? {
        didSet {
            oldValue?.removeFromSuperview()
            if let titleView = titleView {
                addSubview(titleView)
                titleView.snp.makeConstraints { make in
                    make.centerX.equalToSuperview()
                    make.centerY.equalToSuperview().offset(statusBarHeight/2)
                    make.width.lessThanOrEqualToSuperview().multipliedBy(0.6)
                }
            }
        }
    }
    
    // 背景图片
    var backgroundImage: UIImage? {
        didSet {
            backgroundImageView.image = backgroundImage
            backgroundImageView.isHidden = backgroundImage == nil
        }
    }
    
    // 左右按钮数组
    var leftButtons: [UIButton] = [] {
        didSet {
            updateButtons(oldButtons: oldValue, newButtons: leftButtons, stackView: leftButtonsStackView)
        }
    }
    
    var rightButtons: [UIButton] = [] {
        didSet {
            updateButtons(oldButtons: oldValue, newButtons: rightButtons, stackView: rightButtonsStackView)
        }
    }
    
    // 按钮点击回调
    var onLeftButtonTapped: ((Int) -> Void)?
    var onRightButtonTapped: ((Int) -> Void)?
    
    // MARK: - UI Components
    
    private lazy var backgroundImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        iv.isHidden = true
        return iv
    }()
    
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
        label.textColor = .darkText
        label.textAlignment = .center
        return label
    }()
    
    private lazy var leftButtonsStackView: UIStackView = {
        let sv = UIStackView()
        sv.axis = .horizontal
        sv.spacing = 12
        sv.alignment = .center
        sv.distribution = .fill
        return sv
    }()
    
    private lazy var rightButtonsStackView: UIStackView = {
        let sv = UIStackView()
        sv.axis = .horizontal
        sv.spacing = 12
        sv.alignment = .center
        sv.distribution = .fill
        return sv
    }()
    
    private lazy var bottomLine: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        return view
    }()
    
    // MARK: - Initialization
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    // MARK: - Setup
    
    private func setupUI() {
        backgroundColor = .white
        
        addSubview(backgroundImageView)
        addSubview(contentView)
        contentView.addSubview(leftButtonsStackView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(rightButtonsStackView)
        addSubview(bottomLine)
        
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(statusBarHeight)
            make.bottom.equalToSuperview()
        }
        
        leftButtonsStackView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.height.equalTo(44)
        }
        
        rightButtonsStackView.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.height.equalTo(44)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.left.greaterThanOrEqualTo(leftButtonsStackView.snp.right).offset(8)
            make.right.lessThanOrEqualTo(rightButtonsStackView.snp.left).offset(-8)
        }
        
        bottomLine.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    // MARK: - Private Methods
    
    private func updateButtons(oldButtons: [UIButton], newButtons: [UIButton], stackView: UIStackView) {
        // 移除旧按钮
        oldButtons.forEach { button in
            button.removeFromSuperview()
            button.removeTarget(self, action: nil, for: .touchUpInside)
        }
        
        // 清除堆栈视图
        stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
        
        // 添加新按钮
        newButtons.enumerated().forEach { index, button in
            stackView.addArrangedSubview(button)
            
            // 为按钮添加点击事件
            button.tag = index
            if stackView == leftButtonsStackView {
                button.addTarget(self, action: #selector(leftButtonTapped(_:)), for: .touchUpInside)
            } else {
                button.addTarget(self, action: #selector(rightButtonTapped(_:)), for: .touchUpInside)
            }
        }
    }
    
    // MARK: - Button Actions
    
    @objc private func leftButtonTapped(_ sender: UIButton) {
        onLeftButtonTapped?(sender.tag)
    }
    
    @objc private func rightButtonTapped(_ sender: UIButton) {
        onRightButtonTapped?(sender.tag)
    }
    
    // MARK: - Public Methods
    
    /// 隐藏底部线条
    func hideBottomLine(_ hide: Bool) {
        bottomLine.isHidden = hide
    }
}
